//
//  BNDOfficeDocAlert.m
//  OfficeDoc
//
//  Created by pie on 2022/4/17.
//  Copyright © 2022 pie. All rights reserved.
//

#import "BNDOfficeDocAlert.h"

const CGFloat kAlertWidth = 295.0f;
const CGFloat kAlertHeight = 164.0f;
const CGFloat kBetweenLabelOffset = 20.0f;
const CGFloat kSingleButtonWidth = 160.0f;
const CGFloat kCoupleButtonWidth = 100.0f;
const CGFloat kButtonHeight = 44.0f;
const CGFloat kButtonBottomOffset = 18.0f;

@interface BNDOfficeDocAlert ()

@property (nonatomic, strong) UILabel   *titleLabel;
@property (nonatomic, strong) UILabel   *alertContentLabel;
@property (nonatomic, strong) UIButton  *leftBtn;
@property (nonatomic, strong) UIButton  *rightBtn;
@property (nonatomic, strong) UIView    *backView;
@property (nonatomic, strong) UIView    *transparentView;
@property (nonatomic, copy  ) void(^actionHandler)(LKCustomButton clickNum);  /**<关闭回调>*/
@property (nonatomic, strong) UIView    *baseView;
@end

@implementation BNDOfficeDocAlert

- (instancetype)initWithTitle:(NSString *)content
              leftButtonTitle:(NSString *)leftTitle
             rightButtonTitle:(NSString *)rigthTitle
                  clickAction:(void (^)(LKCustomButton clickNum))actionHandler
{
    self = [super init];
    if (self) {
       
        self.actionHandler = actionHandler;
        self.backgroundColor = UIColorWithHexAndAlpha(0x000000, 0.6);
        [self addSubview:self.backView];
        [self.backView addSubview:self.transparentView];
        [self.transparentView addSubview:self.alertContentLabel];
        [self.leftBtn setTitle:leftTitle forState:UIControlStateNormal];
        [self.rightBtn setTitle:rigthTitle forState:UIControlStateNormal];
        [self.transparentView addSubview:self.leftBtn];
        [self.transparentView addSubview:self.rightBtn];
        [self.transparentView addSubview:self.titleLabel];
        self.alertContentLabel.text = content;
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, kAlertHeight-45, kAlertWidth, 0.5)];
        lineView.backgroundColor = UIColorWithHex(0xE2E4EA);
        [self.transparentView addSubview:lineView];
        
        UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(kAlertWidth/2, kAlertHeight-45, 0.5, 45)];
        lineView1.backgroundColor = UIColorWithHex(0xE2E4EA);
        [self.transparentView addSubview:lineView1];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    }
    return self;
}

#pragma mark - Getter

- (UILabel*)alertContentLabel
{
    if(!_alertContentLabel)
    {
        CGFloat contentLabelWidth = kAlertWidth - 40;
        _alertContentLabel = [[UILabel alloc] initWithFrame:CGRectMake((kAlertWidth - contentLabelWidth) * 0.5, 60, contentLabelWidth, 45)];
        _alertContentLabel.numberOfLines = 2;
        _alertContentLabel.textAlignment = NSTextAlignmentCenter;
        _alertContentLabel.textColor = UIColorWithHex(0x757A82);
        _alertContentLabel.font = BNDPFRegularFont(14);
        
    }
    
    return _alertContentLabel;
}


- (UILabel*)titleLabel
{
    if(!_titleLabel)
    {
        CGFloat contentLabelWidth = kAlertWidth - 16;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((kAlertWidth - contentLabelWidth) * 0.5, kButtonBottomOffset, contentLabelWidth, 25)];
        _titleLabel.numberOfLines = 1;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = UIColorWithHex(0x34363B);
        _titleLabel.font = BNDPFMediumFont(18);
        _titleLabel.text = @"实名认证提醒";
    }
    
    return _titleLabel;
}


- (UIButton*)rightBtn
{
    if(!_rightBtn)
    {
        CGRect  rightBtnFrame = CGRectMake(kAlertWidth/2, kAlertHeight-44, kAlertWidth/2, kButtonHeight);
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = rightBtnFrame;;
        _rightBtn.titleLabel.font = BNDPFMediumFont(14);
        [_rightBtn setTitleColor:UIColorWithHex(0x3770EB) forState:UIControlStateNormal];
        [_rightBtn setTitleColor:UIColorHexAndAlpha(0x3770EB, 0.5) forState:UIControlStateHighlighted];
        [_rightBtn addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _rightBtn.tag = LKCustomButtonRight;
    }
    return _rightBtn;
}

- (UIButton*)leftBtn
{
    if(!_leftBtn)
    {
        CGRect leftBtnFrame = CGRectMake(0, kAlertHeight-44, kAlertWidth/2, kButtonHeight);
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftBtn.frame = leftBtnFrame;
        _leftBtn.titleLabel.font = BNDPFRegularFont(14);
        [_leftBtn setTitleColor:UIColorWithHex(0x34363B) forState:UIControlStateNormal];
        [_leftBtn setTitleColor:UIColorHexAndAlpha(0x34363B, 0.5) forState:UIControlStateHighlighted];
        [_leftBtn addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _leftBtn.tag = LKCustomButtonLeft;
    }
    return _leftBtn;
}

- (UIView*)backView
{
    if(!_backView)
    {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor clearColor];
    }
    return _backView;
}

- (UIView*)transparentView
{
    if(!_transparentView)
    {
        _transparentView = [[UIView alloc] init];
        _transparentView.backgroundColor = [UIColor whiteColor];
        _transparentView.layer.cornerRadius = 14.0;
        _transparentView.frame = CGRectMake(0, 60, kAlertWidth,kAlertHeight);
    }
    return _transparentView;
}

#pragma mark - method

- (void)leftBtnClicked:(id)sender
{
    [self dismissAlert];
    if (self.actionHandler) {
        self.actionHandler(LKCustomButtonLeft);
    }
}

- (void)rightBtnClicked:(id)sender
{
    [self dismissAlert];
    if (self.actionHandler) {
        self.actionHandler(LKCustomButtonRight);
    }
}

- (void)show:(UIView*)displayView
{
    self.frame = displayView.frame;
    self.backView.frame = CGRectMake(ceil((CGRectGetWidth(displayView.bounds) - kAlertWidth) * 0.5), 0, kAlertWidth, kAlertHeight+60);
    [displayView addSubview:self];
    self.baseView = displayView;
    self.backView.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.backView.transform = CGAffineTransformMakeRotation(0);
        self.backView.frame = CGRectMake(ceil((CGRectGetWidth(displayView.bounds) - kAlertWidth) * 0.5), (CGRectGetHeight(displayView.bounds) - kAlertHeight -120)*0.5, kAlertWidth, kAlertHeight+60);
    } completion:^(BOOL finished) {
     
    }];
}

- (void)dismissAlert
{
    [self removePopoverView];
}

- (void)removePopoverView
{
    CGRect afterFrame = CGRectMake(ceil((CGRectGetWidth(self.baseView.bounds) - kAlertWidth) * 0.5), CGRectGetHeight(self.baseView.bounds), kAlertWidth, kAlertHeight+60);
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.backView.frame = afterFrame;
        self.backView.transform = CGAffineTransformMakeRotation(-M_1_PI / 1.5);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
